<template>
  <div class="com-container">
    <div class="com-chart" id="hot"></div>
    <span class="iconfont left" @click="toLeft" :style="comStyle"
      >&#xe6eb;</span
    >
    <span class="iconfont right" @click="toRight" :style="comStyle"
      >&#xe6ee;</span
    >
    <span class="title" :style="comStyle">{{ title }}</span>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getThemeValue } from '../utils/theme_utils'
export default {
  data () {
    return {
      chartId: null,
      allData: null,
      activeIndex: 0,
      titleFontSize: 0
    }
  },
  computed: {
    ...mapState(['theme']),
    title () {
      if (this.allData) {
        return this.allData[this.activeIndex].name
      } else {
        return ''
      }
    },
    comStyle () {
      return {
        fontSize: this.titleFontSize + 'px',
        color: getThemeValue(this.theme).titleColor
      }
    }
  },
  methods: {
    toLeft () {
      this.activeIndex--
      if (this.activeIndex < 0) {
        this.activeIndex = this.allData.length - 1
      }
      this.updateCharts()
    },
    toRight () {
      this.activeIndex++
      if (this.activeIndex > this.allData.length - 1) {
        this.activeIndex = 0
      }
      this.updateCharts()
    },
    // 初始化
    initCharts () {
      this.chartId = this.$echarts.init(
        document.getElementById('hot'),
        this.theme
      )
      const initOption = {
        title: {
          text: '▎ 热销商品销售金额占比统计',
          left: 20,
          top: 20
        },
        tooltip: {
          // 提示框
          trigger: 'item',
          formatter: (params) => {
            const tipArray = []
            params.data.children.forEach(function (item) {
              const childStr = ` ${item.name}&nbsp;&nbsp;&nbsp; ${
                parseInt((item.value / params.value) * 100) + '%'
              } `
              tipArray.push(childStr)
            })
            return tipArray.join('<br/>')
          }
        },
        // 图例的样式
        legend: { top: '15%', icon: 'circle' },
        series: [
          {
            center: ['50%', '50%'],
            type: 'pie',
            label: {
              // 隐藏标题
              show: false
            },
            labelLine: {
              // 隐藏标题线
              show: false
            },
            emphasis: {
              label: {
                // 高亮显示文字
                show: true
              }
            }
          }
        ]
      }
      this.chartId.setOption(initOption)
    },
    // 获取数据
    async getData () {
      const { data: res } = await this.$http.get('hot')
      this.allData = res
      this.updateCharts()
    },
    // 屏幕适配
    screenAdapter () {
      this.titleFontSize =
        (document.getElementById('hot').offsetWidth / 100) * 3.6
      const resizeOption = {
        title: { textStyle: { fontSize: this.titleFontSize } },
        series: [{ radius: this.titleFontSize * 4.5, center: ['50%', '60%'] }],
        legend: {
          itemWidth: this.titleFontSize,
          itemHeight: this.titleFontSize,
          itemGap: this.titleFontSize / 2,
          textStyle: { fontSize: this.titleFontSize / 2 }
        }
      }
      this.chartId.setOption(resizeOption)
      this.chartId.resize()
    },
    // 数据更新
    updateCharts () {
      // 图标所需数据
      const serveData = this.allData[this.activeIndex].children.map((item) => {
        return {
          value: item.value,
          name: item.name,
          children: item.children
        }
      })
      // 图例所需数据
      const legendData = this.allData[this.activeIndex].children.map((item) => {
        return item.name
      })
      const dataOption = {
        legend: { data: legendData },
        series: [{ data: serveData }]
      }
      this.chartId.setOption(dataOption)
    }
  },
  mounted () {
    this.initCharts()
    this.getData()
    window.addEventListener('resize', this.screenAdapter)
    this.screenAdapter()
  },
  destroyed () {
    window.removeEventListener('resize', this.screenAdapter)
  },
  watch: {
    // 监听主题的变化  主题变化后更新图表
    theme () {
      this.chartId.dispose() // 重新渲染组件
      this.initCharts()
      this.screenAdapter()
      this.updateCharts()
    }
  }
}
</script>

<style lang='less' scoped>
.left {
  position: absolute;
  top: 50%;
  left: 10%;
  color: #fff;
  //   transform: translateY(-50%);
  cursor: pointer;
}
.right {
  position: absolute;
  top: 50%;
  right: 10%;
  color: #fff;
  //   transform: translateY(-50%);
  cursor: pointer;
}
.title {
  position: absolute;
  left: 80%;
  bottom: 20px;
  font-weight: bold;
  color: #fff;
}
</style>
